#
# This file creates the <libname>.lib.a files specified at the command line.
#
# The following variables must be defined by the caller:
#
#   VERBOSE          - controls the make verboseness
#   VERBOSE_DIR      - verboseness of directory change messages
#   VERBOSE_MK       - verboseness of make calls
#   REPOSITORIES     - source code repositories to use
#   BASE_DIR         - base directory of build system repository
#   TARGET_DIR       - target build directory
#   BUILD_BASE_DIR   - build directory with build config
#   LIB_CACHE_DIR    - destination directory for object files
#   LIB_PROGRESS_LOG - library build log file
#

#
# Determine the set of library files from the command line arguments
#
LIB_FILES_TO_BUILD = $(addprefix $(LIB_CACHE_DIR)/,$(filter-out single_lib_target,$(MAKECMDGOALS)))

#
# If called without a target, build nothing at all.
# We execute 'true' to prevent the "nothing to be done" message.
#
all:
	@true

#
# For each specified <libname>.lib.a file, we create a symbolic
# link to the corresponding object file in LIB_CACHE_DIR.
#
%.lib.a: $(LIB_CACHE_DIR)/%.lib.a
	$(VERBOSE)ln -sf $^ $(TARGET_DIR) 2>/dev/null || true

.PHONY: $(LIB_FILES_TO_BUILD)

#
# For each <libname>.lib.a file, this rule is called to build the file.
# The PHONY tag makes sure that we revisit each library.
#
ifneq ($(LIB_FILES_TO_BUILD),)
$(LIB_FILES_TO_BUILD): $(LIB_CACHE_DIR)
	$(VERBOSE_MK)$(MAKE) $(VERBOSE_DIR)  -f $(BASE_DIR)/mk/lib.mk \
       -C $(LIB_CACHE_DIR) \
	  LIB_O_TARGET=$@ \
	  REPOSITORIES="$(REPOSITORIES)" \
	  BASE_DIR=$(realpath $(BASE_DIR)) \
	  BUILD_BASE_DIR=$(BUILD_BASE_DIR) \
	  TARGET_DIR=$(TARGET_DIR) \
	  LIB_PROGRESS_LOG=$(LIB_PROGRESS_LOG) \
	  VERBOSE=$(VERBOSE) \
	  VERBOSE_DIR=$(VERBOSE_DIR) \
	  VERBOSE_MK=$(VERBOSE_MK)
endif

#
# Create directory for libcache if needed
#
$(LIB_CACHE_DIR):
	$(VERBOSE)mkdir -p $@
